package com.jiantong.web.open.app.work;

import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.RetailBreedRecord;
import com.jiantong.work.service.RetailBreedRecordReadService;
import com.jiantong.work.service.RetailBreedRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 25/7/17.
 *
 */
@Slf4j
@OpenBean
public class RetailBreedRecordApis {

    @RpcConsumer
    private RetailBreedRecordWriteService retailBreedRecordWriteService;

    @RpcConsumer
    private RetailBreedRecordReadService retailBreedRecordReadService;


    @OpenMethod(
            key = "find.retail.breed.record.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public RetailBreedRecord findById(Long id){

        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"id should be not null");
        }

        Response<RetailBreedRecord> response = retailBreedRecordReadService.findById(id);

        if(!response.isSuccess()){
            log.error("failed to find retail breed record , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "paging.retail.breed.record.for.a.single",
            paramNames = {"pageNo","pageSize","status","breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<RetailBreedRecord> paging(Integer pageNo, Integer pageSize ,Integer status ,Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"breedFactoryId should be not null");
        }

        Response<Paging<RetailBreedRecord>> response = retailBreedRecordReadService.paging(pageNo,pageSize,status,breedFactoryId);

        if(!response.isSuccess()){
            log.error("failed to paging retail breed record , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "paging.retail.breed.record.for.multi",
            paramNames = {"pageNo","pageSize","status","breedFactoryId","breedType","villageId","fromDate","toDate"},
            httpMethods = RequestMethod.GET
    )
    public Paging<RetailBreedRecord> pagingMulti(Integer pageNo, Integer pageSize ,
                                                 Integer status ,Long breedFactoryId,
                                                 Integer breedType, Long villageId,
                                                 String fromDate, String toDate){
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }

        Response<Paging<RetailBreedRecord>> response =
                retailBreedRecordReadService.pagingMulti(pageNo,pageSize,status,breedFactoryId,breedType,villageId,start,end);

        if(!response.isSuccess()){
            log.error("failed to paging retail breed record for multi , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();

    }

    @OpenMethod(
            key = "create.retail.breed.record",
            paramNames = {"retailBreedRecord","toStatus"},
            httpMethods = RequestMethod.POST
    )
    public Long create(RetailBreedRecord retailBreedRecord , Integer toStatus){
        if(retailBreedRecord.getBreedFactoryId() == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"breedFactoryId should be not null");
        }

        Response<Long> response = retailBreedRecordWriteService.create(retailBreedRecord,toStatus);

        if(!response.isSuccess()) {
            log.error("failed to create retail breed record , case = {}", response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(), response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "update.retail.breed.record",
            paramNames = {"retailBreedRecord","toStatus"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(RetailBreedRecord retailBreedRecord , Integer toStatus){
        if(retailBreedRecord.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"id should be not null");
        }

        if(retailBreedRecord.getStatus() == null){
            log.error("status should be not null");
            throw new OPClientException(ExceptionCode.PARAMETER_NOT_EXIST.value(),"status should be not null");
        }

        Response<Map<String ,Integer>> statusMap = retailBreedRecordReadService.findStatusById(retailBreedRecord.getId());
        if(statusMap.isSuccess() && !retailBreedRecord.getStatus().equals(statusMap.getResult().get("status"))){
            log.error("retailBreedRecord is already submitted");
            throw new OPClientException(ExceptionCode.DOCUMENT_REPEAT_SUBMIT.value(),"retailBreedRecord is already submitted");
        }

        Response<Boolean> response = retailBreedRecordWriteService.update(retailBreedRecord,toStatus);

        if(!response.isSuccess()){
            log.error("failed to update retail breed record , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),response.getError());
        }
        return response.getResult();
    }


    /**
     * 在当前登录人(协检员)所管辖的养殖场中,按照村id查找一组养殖场
     * @param pageNo
     * @param pageSize
     * @param status
     * @param breedFactoryId
     * @param breedType
     * @param villageId
     * @param fromDate
     * @param toDate
     * @return
     */
    @OpenMethod(
            key = "paging.retail.breed.record.for.multi2",
            paramNames = {"pageNo","pageSize","status","breedFactoryId","breedType","villageId","fromDate","toDate"},
            httpMethods = RequestMethod.GET
    )
    public Paging<RetailBreedRecord> pagingMulti2(Integer pageNo, Integer pageSize ,
                                                 Integer status ,Long breedFactoryId,
                                                 Integer breedType, Long villageId,
                                                 String fromDate, String toDate){
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }

        Response<Paging<RetailBreedRecord>> response =
                retailBreedRecordReadService.pagingByVillage(pageNo,pageSize,status,breedFactoryId,breedType,villageId,start,end);

        if(!response.isSuccess()){
            log.error("failed to paging retail breed record for multi , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();

    }



}
